******************************************************************************************
**** Cropping frequency and area response to climate shocks can exceed yield response ****
******************************************************************************************

**************************    DATA PREPARATION    ****************************************
use data, clear
foreach month in "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec" {
gen st_`month'=t_`month'*t_`month'
gen sp_`month'=p_`month'*p_`month'
}
foreach month in "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec" {
gen origp_`month' = p_`month' 
gen origsp_`month' = sp_`month' 
gen origt_`month' = t_`month' 
gen origst_`month' = st_`month' 
}
tab mcacode, gen(dmca)
tab year, gen(dyear)
drop dmca1
drop dyear1
save data2, replace
use data2, clear
collapse t_* p_*, by(mcacode)
foreach month in "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec" {
rename p_`month' `month'aprec 
rename t_`month' `month'atemp 
}
save averages, replace
use data2, clear
merge m:m mcacode using averages, nogen
erase averages.dta
save mcaFINAL_with_avgs, replace
clear

